<template>
  <el-container>
    <el-header style="margin-bottom: 30px;">
      <el-button type="primary" icon="el-icon-circle-plus" @click="addNewCompany" style="margin: 10px;">新增</el-button>
      <el-form :inline="true" :model="form">
        <el-form-item><el-input v-model="form.short_name" placeholder="请输入公司简称"></el-input></el-form-item>
        <el-form-item><el-input v-model="form.name" placeholder="请输入公司名称"></el-input></el-form-item>
        <el-form-item>
          <el-select v-model="form.status" placeholder="请选择">
            <el-option
              v-for="(item,k) in company.statusList"
              :key="k"
              :label="item"
              :value="k">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item><el-button type="primary" @click="getList">搜索</el-button></el-form-item>
      </el-form>
    </el-header>

    <el-main>
      <el-table
        :data="company.companyList"
        tooltip-effect="dark"
        style="width: 100%"
      >
<!--        简称-->
        <el-table-column label="简写字母">
         <template slot-scope="scope">
           <el-input v-model="scope.row.short_name" placeholder="输入简写字母" maxlength="8"></el-input>
         </template>
        </el-table-column>

        <!--        名称-->
        <el-table-column label="公司简称" min-width="100px">
          <template slot-scope="scope">
            <el-input v-model="scope.row.name" placeholder="输入公司简称" maxlength="20"></el-input>
          </template>
        </el-table-column>

        <el-table-column label="税率(%)" width="70px">
          <template slot-scope="scope">
            <el-input v-model="scope.row.tax" placeholder="输入税率" maxlength="2" step="1" type="number"></el-input>
          </template>
        </el-table-column>

        <el-table-column label="部门" min-width="150px">
          <template slot-scope="scope">
            <el-select v-model="scope.row.deptid" multiple >
              <el-option :label="vv.title" :value="vv.id" v-for="(vv,k) in company.dept_list" :key="k"></el-option>
            </el-select>
          </template>
        </el-table-column>

        <el-table-column label="行政" min-width="100px">
          <template slot-scope="scope">
            <el-select v-model="scope.row.xzid">

              <el-option :label="vv.username" :value="vv.id" v-for="(vv,k) in company.xz_list" :key="k"></el-option>
            </el-select>
          </template>
        </el-table-column>

        <el-table-column label="采购经理"  min-width="100px">
          <template slot-scope="scope">
            <el-select v-model="scope.row.cgjlid">
              <el-option :label="vv.username" :value="vv.id" v-for="(vv,k) in company.cg_list" :key="k"></el-option>
            </el-select>
          </template>
        </el-table-column>

        <el-table-column label="采购总监"  min-width="100px">
          <template slot-scope="scope">
            <el-select v-model="scope.row.cgid">
              <el-option :label="vv.username" :value="vv.id" v-for="(vv,k) in company.cgzj_list" :key="k"></el-option>
            </el-select>
          </template>
        </el-table-column>

        <!--        公司副总-->
        <el-table-column label="副总"  min-width="100px">
          <template slot-scope="scope">
            <el-select v-model="scope.row.fzid">

              <el-option :label="vv.username" :value="vv.id" v-for="(vv,k) in company.fzjl_list" :key="k"></el-option>
            </el-select>
          </template>
        </el-table-column>

        <!--        公司副总-->
        <el-table-column label="总经理"  min-width="100px">
          <template slot-scope="scope">
            <el-select v-model="scope.row.zjlid">

              <el-option :label="vv.username" :value="vv.id" v-for="(vv,k) in company.zjl_list" :key="k"></el-option>
            </el-select>
          </template>
        </el-table-column>

<!--        公司财务-->
        <el-table-column label="财务总监"  min-width="100px">
          <template slot-scope="scope">
            <el-select v-model="scope.row.cwid">

              <el-option :label="vv.username" :value="vv.id" v-for="(vv,k) in company.cw_list" :key="k"></el-option>
            </el-select>
          </template>
        </el-table-column>


        <el-table-column label="出纳"  min-width="100px">
          <template slot-scope="scope">
            <el-select v-model="scope.row.cnid">

              <el-option :label="vv.username" :value="vv.id" v-for="(vv,k) in company.cn_list" :key="k"></el-option>
            </el-select>
          </template>
        </el-table-column>

        <el-table-column label="审计"  min-width="100px">
          <template slot-scope="scope">
            <el-select v-model="scope.row.sjid">
              <el-option :label="vv.username" :value="vv.id" v-for="(vv,k) in company.sj_list" :key="k"></el-option>
            </el-select>
          </template>
        </el-table-column>

        <!--        事业部总监-->
        <el-table-column label="事业部总监"  min-width="100px">
          <template slot-scope="scope">
            <el-select v-model="scope.row.syzjid">

              <el-option :label="vv.username" :value="vv.id" v-for="(vv,k) in company.sybid_list" :key="k"></el-option>
            </el-select>
          </template>
        </el-table-column>

        <el-table-column label="状态"  min-width="100px">
          <template slot-scope="scope">
            <el-select v-model="scope.row.status">
              <el-option :label="vv" :value="k" v-for="(vv,k) in company.statusList" :key="k"></el-option>
            </el-select>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="80px">
          <template slot-scope="scope">
            <el-button type="primary"  size="mini" @click="editCompany(scope.row)"> 编辑</el-button>
          </template>
        </el-table-column>

      </el-table>
    </el-main>

    <el-footer>
      <!--分页-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="form.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="form.page_size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="company.total">
      </el-pagination>
    </el-footer>

<!--    弹窗-->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" >
      <el-form :model="companyData" ref="companyData">
        <el-form-item label="简写字母" :label-width="formLabelWidth" prop="short_name">
          <el-input v-model="companyData.short_name"></el-input>
        </el-form-item>
        <el-form-item label="公司简称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="companyData.name"></el-input>
        </el-form-item>
        <el-form-item label="税率(%)" :label-width="formLabelWidth" prop="tax">
          <el-input v-model="companyData.tax" placeholder="输入税率" maxlength="2" step="1" type="number"></el-input>
        </el-form-item>

        <el-form-item label="部门" :label-width="formLabelWidth"  prop="deptid">
          <el-select v-model="companyData.deptid" multiple clearable>
            <el-option :label="vv.title" :value="vv.id" v-for="(vv,k) in company.dept_list" :key="k"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="行政" prop="xzid" :label-width="formLabelWidth">
          <el-select v-model="companyData.xzid">
            <el-option :label="vv.username" :value="vv.id" v-for="(vv,k) in company.xz_list" :key="k"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="采购经理" :label-width="formLabelWidth"  prop="cgjlid">
          <el-select v-model="companyData.cgjlid">
            <el-option :label="vv.username" :value="vv.id" v-for="(vv,k) in company.cg_list" :key="k"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="采购总监" :label-width="formLabelWidth"  prop="cgid">
          <el-select v-model="companyData.cgid">
            <el-option :label="vv.username" :value="vv.id" v-for="(vv,k) in company.cgzj_list" :key="k"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="副总" :label-width="formLabelWidth"  prop="fzid">
          <el-select v-model="companyData.fzid">
            <el-option :label="vv.username" :value="vv.id" v-for="(vv,k) in company.fzjl_list" :key="k"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="总经理" :label-width="formLabelWidth"  prop="zjlid">
          <el-select v-model="companyData.zjlid">
            <el-option :label="vv.username" :value="vv.id" v-for="(vv,k) in company.zjl_list" :key="k"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="财务总监" :label-width="formLabelWidth"  prop="cwid">
          <el-select v-model="companyData.cwid">
            <el-option :label="vv.username" :value="vv.id" v-for="(vv,k) in company.cw_list" :key="k"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="出纳" :label-width="formLabelWidth"  prop="cnid">
        <el-select v-model="companyData.cnid">
          <el-option :label="vv.username" :value="vv.id" v-for="(vv,k) in company.cn_list" :key="k"></el-option>
        </el-select>
      </el-form-item>

        <el-form-item label="审计" :label-width="formLabelWidth"  prop="sjid">
          <el-select v-model="companyData.sjid">
            <el-option :label="vv.username" :value="vv.id" v-for="(vv,k) in company.sj_list" :key="k"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="事业部总监" :label-width="formLabelWidth"  prop="syzjid">
          <el-select v-model="companyData.syzjid">
            <el-option :label="vv.username" :value="vv.id" v-for="(vv,k) in company.sybid_list" :key="k"></el-option>
          </el-select>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelDialog">取 消</el-button>
        <el-button type="primary" @click="addCompany">确 定</el-button>
      </div>
    </el-dialog>


  </el-container>
</template>

<script>
  import {mapState,mapActions} from 'vuex'
  import {editCompany,delCompany,addCompanyData} from "@/apilist/Company";

  export default {
   name: "setCompany",
    data(){
      return {
          form: {
              short_name: '',
              name: '',
              status: '',
              page: 1,
              page_size: 10,
          },
          dialogFormVisible: false,
          companyData: {
              short_name: '',
              name: '',
              deptid: [],
              xzid: '',
              cgid: '',
              syzjid: '',
              fzid: '',
              zjlid: '',
              cnid: '',
              cgjlid: '',
              sjid: '',
              cwid: '',
          },
          formLabelWidth: '90px',
          title: '新增公司',
      }
    },
    computed:{
       ...mapState("Setdata",["company"])
    },
    methods:{
       ...mapActions("Setdata",["getCompanyListActions"]),
        getList(){
           this.getCompanyListActions(this.form)
        },
        handleSizeChange(val){
            this.form.page_size = val
            this.getList()
        },
        handleCurrentChange(val){
           this.form.page = val
           this.getList()
        },
        //编辑公司
        editCompany(row){
            editCompany(row).then(res => {
                if(res.state == 'ok'){
                    this.$message({
                        type: 'success',
                        message: res.msg
                    })
                }
            })
        },

        //删除公司
        delCompany(row){

        },
        //新增公司
        addNewCompany(){
            this.resetForm()
            this.dialogFormVisible = true
        },

        addCompany(){
            addCompanyData(this.companyData).then(res => {
                if(res.state == 'ok'){
                    this.$message({
                        type: 'success',
                        message: res.msg,
                        onClose: ()=>{
                            this.dialogFormVisible = false
                            this.getList()
                        }
                    })
                }
            })
        },
        cancelDialog(){
            this.resetForm()
            this.dialogFormVisible = false
        },

        resetForm() {
            Object.keys(this.companyData).forEach((key) => {
               if(key == 'deptid') this.companyData[key] = []
                else this.companyData[key] = ''
            })
        },

    },

}
</script>

<style scoped>

</style>
